<?php
namespace app\index\controller;
use app\index\validate\Valid;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use think\Model;
//用户管理
class Users extends Common
{
    /**
     * 管理
     */
    public function manage(){
        $where = [];
        $where['state'] = ['in','0,1'];
        $count = db('users')->where($where)->count();
        $data = db('users')->where($where)->order('created_on desc')->paginate(10,$count);
        $page = $data->render();
        $data = $data->items();
        $maxpage=ceil($count/10);
        foreach($data as $k=>$v){
            $v['role'] = db('role t')->join('users_role t2','t.id=t2.role_id')->where('t2.users_id',$v['id'])->value('role_name');
            $v['em_name'] = db('employee')->where('id',$v['employee_id'])->value('em_name');
            $v['yhlx'] = $v['type']=='1'?'一般用户':'超级用户';
            $v['state'] = $v['state']=='0'?'冻结':'正常';
            $v['power'] = $v['power']=='0'?'不允许':'允许';
            $v['wechat'] = db('employee')->where('id',$v['employee_id'])->value('WeChat');
            $data[$k] = $v;
        }
        $this->assign([
            'page'=>$page,
            'data'=>$data,
            'maxpage'=>$maxpage,
            'count'=>$count
        ]);
        return $this->fetch('index');
    }

    public function add(){
        if($_POST){
            $data = [];
            $data['login_name'] = $this->request->post('login_name');
            $data['employee_id'] = $this->request->post('employee_id');
            $data['type'] = $this->request->post('type');
            $data['state'] = $this->request->post('state');
            $data['power'] = $this->request->post('power');
            $valid = new Valid([
               'login_name|用户名'=>'require|unique:users',
               'employee_id|职员'=>'require|unique:users',
            ]);
            if(!$valid->check($data)){
                echo json_encode(['success'=>false,'msg'=>$valid->getError()]);die;
            }
            $data['login_pwd'] = md5(md5('123456'));;
            $data['created_on'] = date('Y-m-d H:i:s');
            $bool = db('users')->insertGetId($data);
            if(false!==$bool){
                echo json_encode(['success'=>true,'msg'=>'ok']);
            }else{
                echo json_encode(['success'=>false,'msg'=>'error']);
            }
            die;
        }
        return $this->fetch('form');
    }

    public function update(){
        $id = $this->request->param('id');
        if($_POST){
            $data = [];
            $data['id'] = $id;
            $data['employee_id'] = $this->request->post('employee_id');
            $data['type'] = $this->request->post('type');
            $data['state'] = $this->request->post('state');
            $data['power'] = $this->request->post('power');
            $valid = new Valid([
               'employee_id|职员'=>'require|unique:users',
            ]);
            if(!$valid->check($data)){
                echo json_encode(['success'=>false,'msg'=>$valid->getError()]);die;
            }
            $data['login_pwd'] = md5(md5('123456'));;
            $data['created_on'] = date('Y-m-d H:i:s');
            $bool = db('users')->update($data);
            if(false!==$bool){
                echo json_encode(['success'=>true,'msg'=>'ok']);
            }else{
                echo json_encode(['success'=>false,'msg'=>'error']);
            }
            die;
        }
        $data = db('users')->where('id',$id)->find();
        $data['em_name'] = db('employee')->where('id',$data['employee_id'])->value('em_name');
        $this->assign('data',$data);
        return $this->fetch('form');
    }

    //销售人员
    public function bsemployee(){
        $where = [];
        $where['id'] = ['exp','not in(select employee_id from where users)'];
        $data = db('employee')->field('id,em_name')->select();
        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }

    //微信号设置
    public function wxhsz(){
        $id = $this->request->param('id');
        if($_POST){
            $data = [];
            $data['WeChat'] = $this->request->post('WeChat');
            $data['mobile'] = $this->request->post('mobile');
            $data['email'] = $this->request->post('email');
            $bool = db('employee')->where('id',$id)->update($data);
            if(false!==$bool){
                //增加用户的同时增加微信通讯录
                $login_name = db('users')->where('employee_id',$id)->value('login_name');
                $wechat = [];
                $wechat['zh'] = $login_name;
                $wechat['xm'] = db('employee')->where('id',$id)->value('em_name');
                $wechat['wxh'] = $data['WeChat'];
                $wechat['bm'] = 1;
                $wechat['zw'] = db('job t')->join('job_employee t2','t2.job_id=t.id')->where('t2.employee_id',$id)->value('job_name');
                $wechat['sj'] = $data['mobile'];
                $wechat['yx'] = $data['email'];

                if(empty($data['WeChat'])&&empty($data['mobile'])&&empty($data['email'])){
                    db('cg_wechat')->where('zh',$login_name)->delete();
                }else{
                    $cnt = db('cg_wechat')->where('zh',$login_name)->count();
                    if($cnt>0){
                        db('cg_wechat')->where('zh',$login_name)->update($wechat);
                    }else{
                        db('cg_wechat')->insert($wechat);
                    }
                }

                echo json_encode(['success'=>true,'msg'=>'ok']);
            }else{
                echo json_encode(['success'=>false,'msg'=>'error']);
            }
            die;
        }
        $data = db('employee')->where('id',$id)->find();
        $this->assign('data',$data);
        return $this->fetch();
    }

    //解绑微信
    public function jbwx(){
        $id = $this->request->param('id');
        $bool = db('employee')->where('id',$id)->update(['WeChat'=>'']);
        if(false!==$bool){
            $login_name = db('users')->where('employee_id',$id)->value('login_name');
            db('cg_wechat')->where('zh',$login_name)->delete();
            echo json_encode(['success'=>true,'msg'=>'ok']);
        }else{
            echo json_encode(['success'=>false,'msg'=>'error']);
        }
    }

    //重设密码
    public function csmm(){
        $id = $this->request->param('id');
        if($_POST){
            $password = $this->request->post('password');
            $repassword = $this->request->post('repassword');
            $password = trim($password);
            $repassword = trim($repassword);
            if(!$password){
                echo json_encode(['success'=>false,'msg'=>'请输入新密码']);die;
            }
            if(!$repassword){
                echo json_encode(['success'=>false,'msg'=>'请输入确认新密码']);die;
            }
            if($password!=$repassword){
                echo json_encode(['success'=>false,'msg'=>'确认新密码和新密码不一致']);die;
            }
            $password = md5(md5($password));
            $bool = db('users')->where('id',$id)->update(['login_pwd'=>$password]);
            if(false!==$bool){
                echo json_encode(['success'=>true,'msg'=>'ok']);
            }else{
                echo json_encode(['success'=>false,'msg'=>'error']);
            }
            die;
        }
        $data = db('users')->where('id',$id)->find();
        $this->assign('data',$data);
        return $this->fetch();
    }


    //删除
    public function delete(){
        $id = $this->request->param('id');
        $bool = db('users')->where('id',$id)->update(['state'=>-1]);
        if(false!==$bool){
            echo json_encode(['success'=>true,'msg'=>'ok']);
        }else{
            echo json_encode(['success'=>false,'msg'=>'error']);
        }
    }

    //启用
    public function enable(){
        $id = $this->request->param('id');
        $bool = db('users')->where('id',$id)->update(['state'=>1]);
        if(false!==$bool){
            echo json_encode(['success'=>true,'msg'=>'ok']);
        }else{
            echo json_encode(['success'=>false,'msg'=>'error']);
        }
    }

    /**
     * 回收站
     */
    public function isdel(){
        $where = [];
        $where['state'] = -1;
        $count = db('users')->where($where)->count();
        $data = db('users')->where($where)->order('created_on desc')->paginate(10,$count);
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $v['role'] = db('role t')->join('users_role t2','t.id=t2.role_id')->where('t2.users_id',$v['id'])->value('role_name');
            $v['em_name'] = db('employee')->where('id',$v['employee_id'])->value('em_name');
            $v['yhlx'] = $v['type']=='1'?'一般用户':'超级用户';
            $v['state'] = '(已删除)';
            $v['power'] = $v['power']=='0'?'不允许':'允许';
            $v['wechat'] = db('employee')->where('id',$v['employee_id'])->value('WeChat');
            $data[$k] = $v;
        }
        $this->assign([
            'page'=>$page,
            'data'=>$data,
        ]);
        return $this->fetch();
    }
}